package com.iris.live.services.services.impl;

import com.iris.live.services.data.repositories.ItemTranslationRepository;
import com.iris.live.services.services.IItemTranslationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @author: Tony.Zhang
 * @Date: 2017-09-16 21:12
 **/
@Service
@Transactional//(readOnly = true)
public class ItemTranslationServiceImpl implements IItemTranslationService {
    @Autowired
    private ItemTranslationRepository repository;

    public ItemTranslationServiceImpl() {
    }

    @Override
    public String translateToChinese(String type, String item) {
        return repository.getCN(type, item);
    }

    @Override
    public void translateToChinese(String type, Map<String, String> items) {
        List<Map> dictItems = repository.getItems(type);
        Map<String, String> mapItems = new HashMap<>();
        for(Map item : dictItems){
            mapItems.put(item.get("item_en").toString(),item.get("item_cn").toString());
        }

        Set<String> enItems = items.keySet();
        for(String enItem : enItems){
            items.put(enItem, mapItems.get(enItem) == null ? enItem : mapItems.get(enItem).toString());
        }
    }

    @Override
    public String translateToEnglish(String type, String item) {
        return repository.getEN(type, item);
    }

    @Override
    public void translateToEnglish(String type, Map<String, String> items) {
        List<Map> dictItems = repository.getItems(type);
        Map<String, String> mapItems = new HashMap<>();
        for(Map item : dictItems){
            mapItems.put(item.get("item_cn").toString(),item.get("item_en").toString());
        }

        Set<String> cnItems = items.keySet();
        for(String cnItem : cnItems){
            items.put(cnItem, mapItems.get(cnItem) == null ? cnItem : mapItems.get(cnItem).toString());
        }
    }
}
